<?php

/**
 * @author Nguyen Thai Binh
 */
class Setting extends CI_Controller {

  protected $class;
  public $base_lib;

  public function __construct() {
    parent::__construct();
    if (!userdata('username')) {
      redirect('admin/login');
    }
    $this->class = $this->router->fetch_class();
    $this->base_lib = new Base_lib(DB_TABLE_CONFIG);
  }

  public function index() {
    set_userdata('currentURL',  current_url());
    $configs = $this->config_model->get_all();
    $temp = array();
    foreach ($configs as $config) {
      $temp[$config['key']] = $config['value'];
    }
    $this->load->view('admin/' . $this->class . '/edit', array('settings' => $temp));
  }

  public function update() {
    foreach ($_REQUEST as $key => $value) {
      $temp = $this->config_model->get(array('key' => $key));
      if (empty($temp)) {
        $temp = array('id' => null, 'key' => $key);
      }
      $temp['value'] = $value;
      $this->config_model->save($temp);
    }
    set_flash_message($this->lang->line('message.setting.updated'));
    redirect('admin/'.$this->class);
  }

  public function change_language($keyLang = 'default') {    
    set_userdata('currentLang', $keyLang);
    redirect('admin/'.userdata('currentURL'));
  }

}